package org.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
// 添加JUnit测试注解导入
import org.junit.Test; // 修改导入语句

public class ConnectionTest {
    //数据库连接四要素
    private String driverClass = "com.mysql.cj.jdbc.Driver";
    //驱动
    private String url = "jdbc:mysql://127.0.0.1:3306/briup";
    //数据库地址
    private String user = "root"; //用户名
    private String password = "1234"; //密码 修改为正确的密码
    
    //获取数据库连接并输出连接对象
    @Test
    public void test_getConnection() {
        Connection conn = null; // 将变量声明移出try-with-resources
        try {
            Class.forName(driverClass);
            // 使用try-with-resources自动管理资源
            conn = DriverManager.getConnection(url, user, password); // 在try内获取连接
            System.out.println("数据库连接成功: " + conn);
            // 连接成功后可以在这里执行其他数据库操作
        } catch (ClassNotFoundException e) {
            System.err.println("未找到数据库驱动类: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) { // 单独捕获SQLException
            System.err.println("获取数据库连接失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 手动关闭连接
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //测试数据库连接是否有效
    @Test
    public void testConnectionValid() {
        Connection conn = null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, password);
            boolean isValid = conn.isValid(5); // 5秒超时
            System.out.println("数据库连接是否有效: " + isValid);
        } catch (ClassNotFoundException e) {
            System.err.println("未找到数据库驱动类: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("数据库连接测试失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //测试执行简单SQL语句
    @Test
    public void testExecuteSimpleSQL() {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, password);
            stmt = conn.createStatement();
            
            // 执行一个简单的查询语句（这里只是演示，实际可能需要根据数据库内容调整）
            boolean result = stmt.execute("SELECT 1");
            System.out.println("执行SQL语句结果: " + result);
        } catch (ClassNotFoundException e) {
            System.err.println("未找到数据库驱动类: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("执行SQL语句失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 释放资源
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    // 添加main方法便于直接运行测试
    public static void main(String[] args) {
        ConnectionTest test = new ConnectionTest();
        test.test_getConnection();
        test.testConnectionValid();
        test.testExecuteSimpleSQL();
    }
}